/*
 * Copyright 2013 Krzysztof Smigielski
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nilcaream.dirstore.core.processor;

import com.google.inject.assistedinject.Assisted;
import com.nilcaream.dirstore.core.common.ArchiveMapper;
import com.nilcaream.dirstore.core.model.Archive;
import com.nilcaream.dirstore.core.model.ArchiveElement;

import javax.inject.Inject;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;

/**
 * TODO
 * <p/>
 * Krzysztof Smigielski 2/17/13 3:20 PM
 */
public class ArchivingVisitor implements FileVisitor<Path> {

    @Inject
    private ArchiveMapper archiveMapper;

    @Assisted
    @Inject
    private Archive archive;

    @Assisted
    @Inject
    private Path root;

    public FileVisitResult preVisitDirectory(Path directory, BasicFileAttributes attributes) throws IOException {
        if (!directory.equals(root)) {
            ArchiveElement archiveElement = archiveMapper.createFrom(root, directory, attributes);
            archive.getElements().add(archiveElement);
        }
        return FileVisitResult.CONTINUE;
    }

    public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
        ArchiveElement archiveElement = archiveMapper.createFrom(root, file, attributes);
        archive.getElements().add(archiveElement);
        return FileVisitResult.CONTINUE;
    }

    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
//        archive.getIgnoredPaths().add(root.relativize(file).toString());
        return FileVisitResult.CONTINUE;
    }

    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
        return FileVisitResult.CONTINUE;
    }
}
